﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using SIPO.Library;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.Enum;
using SIPO.Library.Interfaces;
using SIPO.Library.DomainObject.Administration.Catalog;

namespace SIPO.Library.DataAccess.Transaction
{
    public partial class PerformanceIndicatorRepo 
    {
        public List<PerformanceIndicator> GetListByResponsibility(int responsibilityID)
        {
            List<PerformanceIndicator> tt = new List<PerformanceIndicator>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT PerformanceIndicatorID, JobDescResponsibilityID, PI, CreatedBy, CreatedDate, UpdatedBy, UpdatedDate FROM transactions.PerformanceIndicator WHERE JobDescResponsibilityID=@JobDescResponsibilityID";
                    cmd.Parameters.Add("@JobDescResponsibilityID", SqlDbType.Int).Value = responsibilityID;
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                PerformanceIndicator t = new PerformanceIndicator();
                                t.PerformanceIndicatorID = dr[PerformanceIndicator.Fields.PerformanceIndicatorID] == DBNull.Value ? 0 : int.Parse(dr[PerformanceIndicator.Fields.PerformanceIndicatorID].ToString());
                                t.JobDescResponsibilityID = dr[PerformanceIndicator.Fields.JobDescResponsibilityID] == DBNull.Value ? 0 : int.Parse(dr[PerformanceIndicator.Fields.JobDescResponsibilityID].ToString());
                                t.PI = dr[PerformanceIndicator.Fields.PI] == DBNull.Value ? string.Empty : dr[PerformanceIndicator.Fields.PI].ToString();
                                t.CreatedBy = dr[PerformanceIndicator.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[PerformanceIndicator.Fields.CreatedBy].ToString();
                                t.CreatedDate = dr[PerformanceIndicator.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[PerformanceIndicator.Fields.CreatedDate].ToString());
                                t.UpdatedBy = dr[PerformanceIndicator.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[PerformanceIndicator.Fields.UpdatedBy].ToString();
                                t.UpdatedDate = dr[PerformanceIndicator.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[PerformanceIndicator.Fields.UpdatedDate].ToString());
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<PerformanceIndicator> GetListByResponsibility(int responsibilityID,int programID)
        {
            List<PerformanceIndicator> tt = new List<PerformanceIndicator>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT PerformanceIndicatorID, JobDescResponsibilityID, PI, CreatedBy, CreatedDate, UpdatedBy, UpdatedDate FROM transactions.PerformanceIndicator WHERE JobDescResponsibilityID=@JobDescResponsibilityID AND ProgramID = @ProgramID";
                    cmd.Parameters.Add("@JobDescResponsibilityID", SqlDbType.Int).Value = responsibilityID;
                    cmd.Parameters.Add("@ProgramID", SqlDbType.Int).Value = programID;
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                PerformanceIndicator t = new PerformanceIndicator();
                                t.PerformanceIndicatorID = dr[PerformanceIndicator.Fields.PerformanceIndicatorID] == DBNull.Value ? 0 : int.Parse(dr[PerformanceIndicator.Fields.PerformanceIndicatorID].ToString());
                                t.JobDescResponsibilityID = dr[PerformanceIndicator.Fields.JobDescResponsibilityID] == DBNull.Value ? 0 : int.Parse(dr[PerformanceIndicator.Fields.JobDescResponsibilityID].ToString());
                                t.PI = dr[PerformanceIndicator.Fields.PI] == DBNull.Value ? string.Empty : dr[PerformanceIndicator.Fields.PI].ToString();
                                t.CreatedBy = dr[PerformanceIndicator.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[PerformanceIndicator.Fields.CreatedBy].ToString();
                                t.CreatedDate = dr[PerformanceIndicator.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[PerformanceIndicator.Fields.CreatedDate].ToString());
                                t.UpdatedBy = dr[PerformanceIndicator.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[PerformanceIndicator.Fields.UpdatedBy].ToString();
                                t.UpdatedDate = dr[PerformanceIndicator.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[PerformanceIndicator.Fields.UpdatedDate].ToString());
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<PerformanceIndicator> GetListByJobDescID(int JobDescriptionID,int programID)
        {
            List<PerformanceIndicator> tt = new List<PerformanceIndicator>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @" SELECT A.PerformanceIndicatorID, A.JobDescResponsibilityID, A.PI, A.CreatedBy, A.CreatedDate, A.UpdatedBy, A.UpdatedDate
                                        FROM transactions.PerformanceIndicator A INNER JOIN transactions.JobDescResponsibility B
                                        ON A.JobDescResponsibilityID =B.ResponsibilityID 
                                        INNER JOIN transactions.JobDescription C ON B.JobDescriptionID = C.JobDescID
                                        WHERE B.JobDescriptionID =@JobDescriptionID AND A.ProgramID = @ProgramID";


                    cmd.Parameters.Add("@JobDescriptionID", SqlDbType.Int).Value = JobDescriptionID;
                    cmd.Parameters.Add("@ProgramID", SqlDbType.Int).Value = programID;
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                PerformanceIndicator t = new PerformanceIndicator();
                                t.PerformanceIndicatorID = dr[PerformanceIndicator.Fields.PerformanceIndicatorID] == DBNull.Value ? 0 : int.Parse(dr[PerformanceIndicator.Fields.PerformanceIndicatorID].ToString());
                                t.JobDescResponsibilityID = dr[PerformanceIndicator.Fields.JobDescResponsibilityID] == DBNull.Value ? 0 : int.Parse(dr[PerformanceIndicator.Fields.JobDescResponsibilityID].ToString());
                                t.PI = dr[PerformanceIndicator.Fields.PI] == DBNull.Value ? string.Empty : dr[PerformanceIndicator.Fields.PI].ToString();
                                t.CreatedBy = dr[PerformanceIndicator.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[PerformanceIndicator.Fields.CreatedBy].ToString();
                                t.CreatedDate = dr[PerformanceIndicator.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[PerformanceIndicator.Fields.CreatedDate].ToString());
                                t.UpdatedBy = dr[PerformanceIndicator.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[PerformanceIndicator.Fields.UpdatedBy].ToString();
                                t.UpdatedDate = dr[PerformanceIndicator.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[PerformanceIndicator.Fields.UpdatedDate].ToString());
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<PerformanceIndicator> GetListByJobDescID(int JobDescriptionID)
        {
            List<PerformanceIndicator> tt = new List<PerformanceIndicator>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = @" SELECT A.PerformanceIndicatorID, A.JobDescResponsibilityID, A.PI, A.CreatedBy, A.CreatedDate, A.UpdatedBy, A.UpdatedDate
                                        FROM transactions.PerformanceIndicator A INNER JOIN transactions.JobDescResponsibility B
                                        ON A.JobDescResponsibilityID =B.ResponsibilityID 
                                        INNER JOIN transactions.JobDescription C ON B.JobDescriptionID = C.JobDescID
                                        WHERE B.JobDescriptionID =@JobDescriptionID ";


                    cmd.Parameters.Add("@JobDescriptionID", SqlDbType.Int).Value = JobDescriptionID;
                  
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                PerformanceIndicator t = new PerformanceIndicator();
                                t.PerformanceIndicatorID = dr[PerformanceIndicator.Fields.PerformanceIndicatorID] == DBNull.Value ? 0 : int.Parse(dr[PerformanceIndicator.Fields.PerformanceIndicatorID].ToString());
                                t.JobDescResponsibilityID = dr[PerformanceIndicator.Fields.JobDescResponsibilityID] == DBNull.Value ? 0 : int.Parse(dr[PerformanceIndicator.Fields.JobDescResponsibilityID].ToString());
                                t.PI = dr[PerformanceIndicator.Fields.PI] == DBNull.Value ? string.Empty : dr[PerformanceIndicator.Fields.PI].ToString();
                                t.CreatedBy = dr[PerformanceIndicator.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[PerformanceIndicator.Fields.CreatedBy].ToString();
                                t.CreatedDate = dr[PerformanceIndicator.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[PerformanceIndicator.Fields.CreatedDate].ToString());
                                t.UpdatedBy = dr[PerformanceIndicator.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[PerformanceIndicator.Fields.UpdatedBy].ToString();
                                t.UpdatedDate = dr[PerformanceIndicator.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[PerformanceIndicator.Fields.UpdatedDate].ToString());
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }		
    }
}
